#include "sys_task.h"
#include "app_adc.h"
#include "app_uart.h"
#include "app_uart_drv.h"
#include "app_etmr.h"
#include "app_exv.h"
#include "app_motors.h"


/***************************************************************************************************
 * function declaration
 **************************************************************************************************/

/*user task function*/ 
void task_adc(void);
void task_1ms(void);
void task_10ms(void);
void task_100ms(void);
void task_200ms(void);
void task_1s(void);

/*events funtion*/


/***************************************************************************************************
 * variable definition
 **************************************************************************************************/
extern volatile uint32_t syscoun;  
uint32_t _syscoun; 
volatile uint32_t syscount;

uint16_t task_cnt1ms = 0;
uint16_t task_cnt10ms = 0;
uint16_t task_cnt100ms = 0;
uint16_t task_cnt10min = 0;

/*
**************************************************************************************************
 * Scheduling function definition
 **************************************************************************************************
 */

void sys_TaskInit(void)
{
    syscoun = 0;
    _syscoun = 0;
    syscount = 0;
    
    task_cnt1ms = 0;
    task_cnt10ms = 0;
    task_cnt100ms = 0;
    task_cnt10min = 0;
                     
    app_adcInit();
    app_uartInit();
    
    app_uartDrvInit();
    
	app_exvInit();
	app_etmr_Motors_Init();
	motors_init();
}

void sys_LoopTask(void)
{
    if (syscoun != _syscoun) {
        _syscoun = syscoun;
        task_adc();
        if ((syscoun & 0xF) == 0) {
            syscount++;
            task_1ms();
            if(syscount%10 ==0) {
                task_10ms();
            }
            if(syscount%100 ==0) {
                syscount = 0;
                task_100ms();
            }
        }
    }
}

/*task functions */
//  1/16 ms
void task_adc(void)
{
	app_Yaw_angle();
	Motor_4Way_Driver();
//	app_Pitch_angle();
//	Mode_Motor_Driver();
}

void task_1ms(void)
{
  app_uartReceiveData();
  app_uartDrvReceiveData();
	//Four_Phase_OutPut();
}

void task_10ms(void)
{

}

void task_100ms(void)
{
    static uint32_t delay;
    task_cnt100ms++;
    if (task_cnt100ms%2==0) {
        task_200ms();
    }
    if (task_cnt100ms%10==0) {
        task_cnt100ms = 0;
        task_1s();
    }
    
}

void task_200ms(void)
{
    
}

void task_1s(void)
{
//    app_sendReceivedInfo();
}


